<template>
<div style="width: 100%;height: 100vh;background-color:slateblue ;overflow: hidden">
  <div style="width: 400px;margin: 150px auto">
    <div style="color: #cccccc;font-size: 30px;text-align: center;padding: 30px 0">欢迎注册</div>
    <el-form ref="from" :model="form" size="normal" :rules="rules" >
      <el-form-item prop="username">
        <el-input :prefix-icon="Avatar" v-model="form.username" ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input :prefix-icon="Lock" v-model="form.password" show-password></el-input>
      </el-form-item>
      <el-form-item prop="confirm">
        <el-input :prefix-icon="Lock" v-model="form.confirm" show-password></el-input>
      </el-form-item>
      <el-form-item >
        <el-button style="width: 100%" type="primary" @click="register">注册</el-button>
      </el-form-item>
      <el-form-item><el-button type="text" @click="$router.push('/login')">&lt;&lt;返回登录 </el-button></el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import request from "@/utils/request";
import {ElMessage} from "element-plus";

export default {
	name: 'Register',
  data(){
    return{
      form:{},
    rules:{
      username: [
        {
          required: true,
          message: '请输入用户名',
          trigger: 'blur',
        },
      ],
      password: [
        {
          required: true,
          message: '请输入密码',
          trigger: 'blur',
        },
          ],
      confirm: [
        {
          required: true,
          message: '请确认密码',
          trigger: 'blur',
        },
      ],
    }
    }
  },
  methods:{
    register(){
      if(this.form.password!=this.form.confirm){
        ElMessage({
          message:'两次密码输入不一致!',
          type:'warning',
        })
        return
      }
      this.$refs['from'].validate((valid) => {
        if (valid) {
          request.post("/user/register",this.form).then(res =>{
            console.log(res);
            if (res.code === '0') {
              ElMessage({
                message: '注册成功',
                type: 'success',
                center: true,
                duration:2000,
              })
              this.$router.push("/login")//登录成功跳转到主页
            } else {
              ElMessage({
                message: res.msg,
                type: 'warning',
                center: true,
                duration:2000,
              });
            }
          })
        }
      })

    }
  }
};
</script>
<script setup>
import {Avatar,Lock} from "@element-plus/icons-vue";
</script>
<style></style>
